package com.weiyin.examination.Fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weiyin.examination.R;
import com.weiyin.examination.activity.QuestionTypeActivity;
import com.weiyin.examination.activity.StartVerificationActivity;
import com.weiyin.examination.application.Constant;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.ToastUtil;
import com.weiyin.examination.view.RoundImageView;

import org.apache.commons.lang3.StringUtils;

/**
 * <pre>
 *     e-mail : niejunfeng@win-electr.com
 *     time   : 2018/04/03
 *     desc   :我的
 *     version: 1.0.0
 *     Copyright: Copyright（c）2017
 *     Company:上海蔚音电子科技有限公司
 * </pre>
 *
 * @author niejunfeng
 */
public class PersonalCenterFragment extends BaseFragment implements View.OnClickListener {
    private View view;
    private LayoutHelper layoutHelper;


    private SharedPreferences sharedPreferences;
    private RelativeLayout meetLayout, shopLayout;

    private RoundImageView userHeadImage;
    private TextView nameTextView;
    private long exitTime = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.personal_fragment_layout, null);
            layoutHelper = new LayoutHelper(getActivity());
            sharedPreferences = getActivity().getSharedPreferences(Constant.USER, 0);

            layoutHelper.scaleView(view);
            initView();
        }
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        String imageUrl = sharedPreferences.getString(Constant.HEAD_IMAGE_URL, "");
        String strName = sharedPreferences.getString(Constant.NICKNAME, "");
        if (!StringUtils.isEmpty(imageUrl)) {
            Glide.with(getActivity()).load(imageUrl).into(userHeadImage);
        } else {
            Log.i("aa", "aaaaaaaaaaaaaa");
            userHeadImage.setImageResource(R.drawable.com_pic_default_head_circle);
        }
        if (!StringUtils.isEmpty(strName)) {
            nameTextView.setText(strName);
        } else {
            nameTextView.setText("请登录");
        }
    }

    public void initView() {
        ImageView back = (ImageView) view.findViewById(R.id.back);
        back.setVisibility(View.GONE);

        nameTextView = (TextView) view.findViewById(R.id.nickName);

        userHeadImage = (RoundImageView) view.findViewById(R.id.userIcon);

        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText("个人中心");


        meetLayout = (RelativeLayout) view.findViewById(R.id.meetLayout);
        shopLayout = (RelativeLayout) view.findViewById(R.id.shopLayout);

        userHeadImage.setOnClickListener(this);
        nameTextView.setOnClickListener(this);
        meetLayout.setOnClickListener(this);
        shopLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.nickName:
            case R.id.userIcon:
                String name = sharedPreferences.getString(Constant.NICKNAME, "");
                if (StringUtils.isEmpty(name)) {
                    startActivity(new Intent(getContext(), StartVerificationActivity.class));
                } else {

                    if ((System.currentTimeMillis() - exitTime) > 2000) {
                        ToastUtil.showTextToast(getContext(), "已登录,在按一次退出登录");
                        exitTime = System.currentTimeMillis();
                    } else {
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.clear();
                        editor.apply();
                        editor.commit();
                        nameTextView.setText("请登录");
                    }
                }

                break;
            case R.id.meetLayout:
                Intent intent = new Intent();
                intent.putExtra("name", "已收费");
                intent.setClass(getContext(), QuestionTypeActivity.class);
                startActivity(intent);
                break;
            case R.id.shopLayout:
                Intent intent1 = new Intent();
                intent1.putExtra("name", "未收费");
                intent1.setClass(getContext(), QuestionTypeActivity.class);
                startActivity(intent1);
                break;
        }
    }
}
